package ext.tianma.part.Interface.MDMInterface.Service;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import ext.tianma.util.PropertiesUtil;

/**
 * 用于发送数据到SOA
 * @author laichao_sun
 *
 */
public class PartInformationServiceToSOA {
	public static String configFilePath = "codebase" + File.separator + "ext" + File.separator + "tianma"
			+ File.separator + "cfg" + File.separator + "TianmaPro.properties";
	
	/**
	 * 用于调用SOA的二级物料的接口发送二级物料信息
	 * 
	 * @return
	 * 
	 * @return
	 */
	public static boolean sendPartInformationBean(String ob) {
		// 打印部件的信息
		String data1 = ob.toString();
		System.out.println("SLC ==主数据部件信息的json:" + data1);
		StringBuffer sb = new StringBuffer();
		try {
			// 创建url资源
			PropertiesUtil ut = new PropertiesUtil(configFilePath);
			//
			String PostUrl = ut.getValueByKey("Part_Information_url");
			System.out.println("MDM主数据接口:PostUrl:"+PostUrl);
			URL url = new URL(PostUrl);
			// 建立http连接
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 设置允许输出
			conn.setDoOutput(true);
			// 设置允许输入
			conn.setDoInput(true);
			// 设置不用缓存
			conn.setUseCaches(false);
			// 设置传递方式
			conn.setRequestMethod("POST");
			// 设置维持长连接
			conn.setRequestProperty("Connection", "Keep-Alive");
			// 设置文件字符集:
			conn.setRequestProperty("Charset", "UTF-8");
			// 转换为字节数组
			byte[] data = (ob.toString()).getBytes();
			// 设置文件长度
			conn.setRequestProperty("Content-Length", String.valueOf(data.length));
			// 设置文件类型:
			conn.setRequestProperty("contentType", "application/json");
			// 开始连接请求
			conn.connect();
			OutputStream out = new DataOutputStream(conn.getOutputStream());

			// 写入请求的字符串
			out.write((ob.toString()).getBytes());
			out.flush();
			out.close();
			System.out.println(conn.getResponseCode());

			// 请求返回的状态
			if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {
				System.out.println("连接成功");
				// 请求返回的数据
				InputStream in1 = conn.getInputStream();
				try {
					String readLine = new String();
					BufferedReader responseReader = new BufferedReader(new InputStreamReader(in1, "UTF-8"));
					while ((readLine = responseReader.readLine()) != null) {
						sb.append(readLine);
					}
					responseReader.close();
					System.out.println("SLC ==调用主数据接口返回的值==>" + sb.toString());

					//二级物料接口返回值的校验
					JSONObject obj;
					obj = new JSONObject((String) sb.toString());
					System.out.println("obj:" + obj);
					if (obj.has("resultCode")) {

						/*
						 * {"resultCode":"1","resultMsg":null}
						 */
						String resultCode = obj.getString("resultCode");
						String resultMsg = obj.getString("resultMsg");
						System.out.println("resultCode:" + resultCode);
						System.out.println("resultMsg:" + resultMsg);
						boolean flag2 = false;
						if ("1".equals(resultCode)) {
							flag2 = true;
						} else {
							flag2 = false;
							String resultData = obj.getString("resultData");
							System.out.println("SLC ==主数据接口发布失败==>失败原因:" + resultData);
						}
						return flag2;
					}	
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			} else {
				System.out.println("调用SOA 主数据接口失败,未连接成功");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;

		}

		return false;

	}
	
	

}
